package com.amc.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amc.configs.Config;
import com.amc.entity.Address;
import com.amc.entity.Customer;
import com.amc.service.IUserService;
import com.amc.utils.ResJsonUtil;
import com.amc.validator.Validator;
import com.amc.validator.impl.PasswordValidator;
import com.amc.validator.impl.UsernameValidator;

@Controller
@RequestMapping("/user")
public class CustomerController {

	IUserService service;

	@Resource(name = "userService")
	public void setService(IUserService service) {
		this.service = service;
	}

	@RequestMapping(value = "/login", method = { RequestMethod.POST })
	@ResponseBody
	public Object login(String username, String password, HttpSession session) {
		Customer customer = service.login(username, password);
		Validator passwordValidate=new PasswordValidator();
		Validator usernameValidate=new UsernameValidator();
		List<String> msg=new ArrayList<>();
		msg.addAll(passwordValidate.validate(password));
		msg.addAll(usernameValidate.validate(username));
		if (customer != null) {
			session.setAttribute("user", customer);
			return ResJsonUtil.responseObject(Config.SUCCESS, customer,
					"SERVER SUCCESS");
		}
		return ResJsonUtil.responseObject(Config.ERROR, msg, "SERVER ERROR");
	}

	@RequestMapping(value = "/regist", method = { RequestMethod.POST })
	@ResponseBody
	public Object register(String username, String password) {
		boolean result = service.register(username, password);
		if (result == true) {
			return ResJsonUtil.responseObject(Config.SUCCESS, "", "");
		}
		return ResJsonUtil.responseObject(Config.ERROR, "", "SERVER ERROR");
	}

	@RequestMapping(value = "/showaddress", method = { RequestMethod.POST })
	@ResponseBody
	public List<Address> showAddress(String customerId) {
		return service.queryAllAddress(customerId);
	}

	@RequestMapping(value = "/updateaddress", method = { RequestMethod.POST })
	@ResponseBody
	public Object updateAddress(String customerId, Address address) {

		if (service.updateAddress(address, customerId)) {
			return ResJsonUtil.responseObject(Config.SUCCESS, service.queryAllAddress(customerId), "");
		} else
			return ResJsonUtil.responseObject(Config.ERROR, "", "SERVER ERROR");
	}

	@RequestMapping(value = "/deleteaddress", method = { RequestMethod.POST })
	@ResponseBody
	public Object deleteAddress(String customerId, String addressId) {
		if (service.deleteAddress(addressId, customerId)) {
			return ResJsonUtil.responseObject(Config.SUCCESS, service.queryAllAddress(customerId), "");
		} else
			return ResJsonUtil.responseObject(Config.ERROR, "", "SERVER ERROR");
	}
	
	@RequestMapping(value = "/addaddress", method = { RequestMethod.POST })
	@ResponseBody
	public Object addAddress(String customerId, Address address) {

		if (service.insertAddress(address, customerId)) {
			return ResJsonUtil.responseObject(Config.SUCCESS, service.queryAllAddress(customerId), "");
		} else
			return ResJsonUtil.responseObject(Config.ERROR, "", "SERVER ERROR");
	}		

}
